<?php

namespace app\api\controller;

use app\api\model\Adv;
use app\api\model\AdvComment;
use app\api\model\AdvLike;
use app\api\model\AdvReply;
use app\api\model\Dynamic;
use app\api\model\DynamicComment;
use app\api\model\DynamicLike;
use app\api\model\DynamicReply;
use app\common\controller\Api;

/**
 * 发现模块
 */
class Find extends Api
{
    protected $noNeedLogin = ['userDynamic'];
    protected $noNeedRight = '*';


    /**
     *
     * 发布广告
     */
    public function publishAdv()
    {
        $param = input();
        $param['user_id'] = $this->uid;
        $user = $this->auth->getUser();
        $total = input('price') * input('num');
        if ((float)$total > (float)$user['gold_coin']) {
            $this->error('金币余额不足');
        }
        $param['total'] = $total;
        $this->success('发布成功', Adv::create($param));
    }

    /**
     *
     * 我的广告
     */
    public function userAdv()
    {
        $user_id = $this->uid;
        $data = Adv::all(['user_id' => $user_id]);
        $this->success('成功', $data);
    }

    /**
     *
     * 发布动态
     */
    public function publishDynamic()
    {
        $param = input();
        $param['user_id'] = $this->uid;
        $this->success('发布成功', Dynamic::create($param));
    }

    /**
     *
     * 评论
     */
    public function dynamicComment()
    {
        $param['content'] = input('content');
        $param['user_id'] = $this->uid;
        if(input('adv_id')){
            $param['adv_id'] = input('adv_id');
            $data = AdvComment::create($param);
            $data['username'] = model('user_info')->where('user_id', $this->uid)->value('username');
        }else{
            $param['reply_id'] = input('reply_id');
            $param['dynamic_id'] = input('dynamic_id');
            $data = DynamicComment::create($param);
            $data['username'] = model('user_info')->where('user_id', $this->uid)->value('username');
            if(input('reply_id')){
                $data['reply_name'] = model('user_info')->where('user_id', input('reply_id'))->value('username');
            }
        }
        $this->success('评论成功', $data);
    }

    /**
     *
     * 回复
     */
    public function dynamicReply()
    {
        $param['content'] = input('content');
        $param['reply_id'] = input('reply_id');
        $param['user_id'] = $this->uid;
//        if(input('adv_comment_id')){
            $param['comment_id'] = input('adv_comment_id');
            $data = AdvReply::create($param);
        $data['username'] = model('user_info')->where('user_id', $this->uid)->value('username');
        $data['reply_name'] = model('user_info')->where('user_id', input('reply_id'))->value('username');
//        }else{
//            $param['comment_id'] = input('comment_id');
//            $data = DynamicReply::create($param);
//        }
        $this->success('评论成功', $data);
    }

    /**
     *
     * 动态点赞
     */
    public function dynamicLike()
    {
        $like_status = input('like_status');
        $param['user_id'] = $this->uid;
        if(input('adv_id')){
            $param['adv_id'] = input('adv_id');
            $data = new AdvLike();
        }else{
            $param['dynamic_id'] = input('dynamic_id');
            $data = new DynamicLike();
        }
        if ($like_status == 0) {
            if($data::get($param))$this->error('点赞失败');
            $this->success('点赞成功', $data::create($param));
        }
        $this->success('取消点赞', $data::destroy($param));
    }

    /**
     *
     * 我的动态
     */
    public function userDynamic()
    {
        $user_id = input('user_id');
        $this->success('请求成功', model('dynamic')->dynamicData($user_id));
    }

    /**
     *
     * 广告圈列表
     */

    public function advList()
    {
        $user_id = $this->uid;
        $search = input('search');
        $city = input('city');
        $this->success('成功', model('adv')->advList($user_id,$city,$search));
    }

    /**
     *
     * 广告圈详情
     */

    public function advDetail()
    {
        $adv_id = input('adv_id');
        $user_id = $this->uid;
        $this->success('成功', model('adv')->advDetail($user_id, $adv_id));
    }

    /**
     *
     * 停止广告
     */

    public function advStop()
    {
        $adv_id = input('adv_id');
        $this->success('成功', model('adv')->where('id', $adv_id)->update(['status' => 3]));
    }


    /**
     *朋友圈列表
     */

    public function friendsList()
    {
        $user_id = $this->uid;
        $this->success('成功', model('friends')->friendsList($user_id));
    }
}